﻿using System;
using System.Data;
using System.Web.UI.WebControls;

using VIT.Web.UI;
using VIT.Web.Controls;
using VIT.Provider;
using VIT.Library.Web;

namespace VIT.Web.Admin
{
    public partial class AlbumTrash : VIT.Web.UI.VITComponent
    {
        VIT.Provider.Album obj;
        protected void Page_Load(object sender, EventArgs e)
        {
            obj = new VIT.Provider.Album();
            pnAlert.Visible = false;
        }

        #region Methods
        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select)]
        public DataTable GetData(string SortColumn)
        {
            return new VIT.Provider.Album().GetRecycleBin(SortColumn);
        }

        public void ClearedList(string[] listId)
        {
            foreach (string id in listId)
            {
                DeleteFile(id);
                obj.Cleared(int.Parse(id));
            }
        }

        private void DeleteFile(string id)
        {
            try
            {
                obj.Id = int.Parse(id);
                string img = obj.GetImage();
                string FileToDelete = string.Empty;
                if (img != "")
                {
                    FileToDelete = Server.MapPath("~" + URL.appPath + "Uploads/Images/Albums/" + img);
                    System.IO.File.Delete(FileToDelete);
                }

            }
            catch
            { }
        }

        private void setTrueVisible(System.Web.UI.HtmlControls.HtmlGenericControl control)
        {
            pnAlert.Visible = true;

            lblChon.Visible = false;
            lblPhucHoiCha.Visible = false;
            lblXoaLanLuot.Visible = false;
            lblPhucHoiLanLuot.Visible = false;
            lblPhucHoiThanhCong.Visible = false;
            lblXoaVinhVien.Visible = false;

            control.Visible = true;
        }
        #endregion

        #region Events
        protected void Delete_Click(object sender, EventArgs e)
        {
            string[] check = Request["itemCheckbox"].Split(',');
            if (check.Length > 0)
            {
                ClearedList(check);
                setTrueVisible(lblXoaVinhVien);
                ListView.DataBind();
            }
            else setTrueVisible(lblChon);
        }

        protected void Back_Click(object sender, EventArgs e)
        {
            string[] check = Request["itemCheckbox"].Split(',');
            if (check.Length == 1)
            {
                if (obj.getParentId(1, int.Parse(check[0])).Length == 0)
                {
                    obj.Delete(0, int.Parse(check[0]));
                    setTrueVisible(lblPhucHoiThanhCong);
                    ListView.DataBind();
                }
                else setTrueVisible(lblPhucHoiCha); // thong bao phai restore nhom cha truoc
            }
            else if (check.Length > 1) setTrueVisible(lblPhucHoiLanLuot); // thong bao xoa tung phan tu
            else setTrueVisible(lblChon); // thong bao chon it nhat 1 phan tu
        }

        protected void Pager_PreRender(object sender, EventArgs e)
        {
            ListView.DataBind();
        }
        #endregion
    }
}
